﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KBManagement.DAL;
using KBManagement.BIZ;
namespace KBManagement.Library
{
  sealed  class Utils
  {
      private static readonly Utils _instance = new Utils();
      private static readonly TransactionLog _instanceLog = new TransactionLog();
      private KBSQLClassDataContext context;
      List<InforObserver> observers;
      List<CustomerObserver> lsObServer;
      List<LoginObserver> lsLoginObserver;
      private Utils()
      {
          context = new KBSQLClassDataContext();
          observers = new List<InforObserver>();
          lsObServer = new List<CustomerObserver>();
          lsLoginObserver = new List<LoginObserver>();
      }

      public void AddAdminObserver(LoginObserver ob)
      {
          lsLoginObserver.Add(ob);
      }

      public void NotificationAdminObserver(Admin admin)
      {
          foreach (var item in lsLoginObserver)
          {
              item.SetInforAdmin(admin);
          }
      }

      public void AddCustomerObserver(CustomerObserver ob)
      {
          lsObServer.Add(ob);
      }

      public void AddInforObserver(InforObserver ob)
      {
          observers.Add(ob);
      }

      public void NotificationCustomerObserver(Customer customer)
      {
          foreach (var item in lsObServer)
          {
              item.UpdateCustomer(customer);
          }
      }

      public void NotificationInforObserver() {
          foreach (var item in observers)
          {
              item.Update();
          }
      }


      public static Utils CreateInstance()
      {
          return _instance;
      }

      public static TransactionLog CreateInstanceLog()
      {
          return _instanceLog;
      }

      public KBSQLClassDataContext GetDBContext() {
          return context;
      }

      public Boolean isNull(string text)
      {
          if (text != String.Empty)
          {
              return false;
          }
          return true;
      }


      public Boolean isNumber(string text)
      {
          try
          {
              Convert.ToInt32(text);
              if (Convert.ToInt32(text) <= 0)
              {
                  return false;
              }
          }
          catch (Exception)
          {
              return false;   
          }
          return true;
      }

      public Boolean isNumberDouble(string text)
      {
          try
          {
              Convert.ToDouble(text);
          }
          catch (Exception)
          {
              return false;
          }
          return true;
      }
    }
}
